package com.hfut.eeg.service;

import com.github.pagehelper.PageHelper;
import com.hfut.eeg.common.util.GenerateID;
import com.hfut.eeg.dao.SetupMapper;
import com.hfut.eeg.dto.DoctorSearchResult;
import com.hfut.eeg.dto.DoctorUserInfo;

import java.net.InetAddress;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * Setup的Service层
 */
@Service
public class SetupService implements GenerateID {
    @Autowired
    SetupMapper setupMapper;
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * 更新用户信息
     * @param doctorUserInfo
     * @return
     */
    public int update(DoctorUserInfo doctorUserInfo)
    {
//        doctorUserInfo.setPasswd(passwordEncoder.encode(doctorUserInfo.getPasswd()));
        return setupMapper.updateMyDoctor(doctorUserInfo)+setupMapper.updateMyUser(doctorUserInfo);
    }

    /**
     * 添加医生信息
     * @param doctorUserInfo
     * @return
     */
    public int addDoctor(DoctorUserInfo doctorUserInfo)
    {
        doctorUserInfo.setPasswd(passwordEncoder.encode(doctorUserInfo.getPasswd()));
        return setupMapper.addDoctor(doctorUserInfo)+setupMapper.addUser(doctorUserInfo);
    }

    /**
     * 删除医生信息
     * @param doctorId
     * @return
     */
    public int deleteDoctor(String doctorId)

    {
        return setupMapper.deleteDoctor(doctorId)+setupMapper.deleteUser(doctorId);
    }

    /**
     * 更新其他医生信息
     * @param doctorUserInfo
     * @return
     */
    public int updateOtherDoctor(DoctorUserInfo doctorUserInfo)
    {
        doctorUserInfo.setPasswd(passwordEncoder.encode(doctorUserInfo.getPasswd()));
        return setupMapper.updateOtherDoctor(doctorUserInfo)+setupMapper.updateOtherUser(doctorUserInfo);
    }

    /**
     * 更新照片信息
     * @param doctorId
     * @param file
     * @return
     * @throws IOException
     */
    public Map updateImg(String doctorId, MultipartFile file) throws IOException {
        String originalName = file.getOriginalFilename();
        File directory = new File("");// 参数为空
        //静态资源一定要存储到编译后的文件夹中，否则会报错误
//        String courseFile = directory.getCanonicalPath();
//        String fullFilename = courseFile+"\\src\\main\\resources\\static\\imgHead\\"+originalName;
//        System.out.println(fullFilename);
        String realPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/imgHead/";
        realPath = realPath.substring(1).replaceAll("/", "\\\\");
        file.transferTo(Paths.get(realPath+originalName));
        InetAddress address = InetAddress.getLocalHost();
        String ip =address.getHostAddress();
        String path = "http://"+ip+":8080"+"/static/imgHead/"+originalName;
        //System.out.println(path);
        Map<String,Object>map = new HashMap<>();
        map.put("doctorId",doctorId);
        map.put("imgHead",path);
        Map<String,Object>map2 = new HashMap<>();
        int i = setupMapper.updateImg(map);
        map2.put("imgHead",path);
        map2.put("value",i);
        return map2;
    }

    /**
     * 获取其他医生信息
     * @param doctorId
     * @return
     */
    public DoctorUserInfo getDoctorUserInfo(String doctorId)
    {
        return setupMapper.getDoctorUserInfo(doctorId);
    }

    /**
     * 分页查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    public List<DoctorSearchResult> getDoctorInfoByPage(Integer pageSize, Integer pageNum)
    {
        PageHelper.startPage(pageNum,pageSize);
        return setupMapper.getDoctorInfoByPage();
    }

    /**
     * 更新密码
     * @param doctorId
     * @param password
     * @return
     */
    public int updatePassword(String doctorId,String password)
    {
        password = passwordEncoder.encode(password);
        Map<String,Object>map=new HashMap<>();
        map.put("doctorId",doctorId);
        map.put("password",password);
        return setupMapper.updatePassword(map);
    }

    @Override
    public String getLatestID() {
        return setupMapper.getDoctorLatestID();
    }
}
